package kr.hkit.powerjava.chapter12;

import java.util.Arrays;
import java.util.Comparator;


class Student implements Comparable<Student>{
	private String stdName;
	private double gpa;
	
	public Student(String stdName, double gpa) {
		this.stdName = stdName;
		this.gpa = gpa;
	}

	public String getStdName() {
		return stdName;
	}

	public double getGpa() {
		return gpa;
	}

	@Override
	public String toString() {
		return " [" + stdName +","+ gpa + "]";
	}

	@Override
	public int compareTo(Student o) {
		
		return (int) (this.gpa-o.gpa);
	}
	
}
public class SortTest {
	public static void main(String[] args) {
		int[] arr = {5,6,7,8,1,2,3,4};
		
		prnArr(arr);
		Arrays.sort(arr);
		prnArr(arr);
		
		Student[] arrStd = new Student[4];
		
		arrStd[0]=new Student("홍길동", 3.3);
		arrStd[1]=new Student("임꺽정", 4.4);
		arrStd[2]=new Student("황진이", 2.6);
		arrStd[3]=new Student("김태희", 1.7);
		
/*		prnArr(arrStd);*/
//		Arrays.sort(arrStd); 평점 기준 정렬
		
		//이름순 정렬
		Comparator<Student> compName = new Comparator <Student>(){
			@Override
			public int compare(Student o1, Student o2){
				
				return o1.getStdName().hashCode()-o2.getStdName().hashCode(); //해쉬코드 글자를 숫자로
			}
		};
		
	
		//평점순 정렬
		Comparator<Student> compGpa = new Comparator<Student>(){
			@Override
			public int compare(Student o1, Student o2){
				return (int) (o1.getGpa()*10-o2.getGpa()*10);
			}
		};
		Arrays.sort(arrStd, compName);
		prnArr(arrStd);
		
		Arrays.sort(arrStd, compGpa);
		prnArr(arrStd);
		
	}
	
	private static void prnArr(Student[] arrStd){
		for(Student s: arrStd){
			System.out.print(s + "\t");
		}
		System.out.println();
	}
		
	
	private static void prnArr(int[] arr) {
		for(int e: arr){
			System.out.printf("%2d", e);
		}
		System.out.println();
	}
}
